//
//  GYComposePictureView.swift
//  Weibo
//
//  Created by vane on 16/8/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
private let GYComposePictureViewCellId = "GYComposePictureViewCellId"
class GYComposePictureView: UICollectionView {

    var imageList:[UIImage] = [UIImage]()
    
    var closure:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addImages(image: UIImage){
        
        self.hidden = false
        
        imageList.append(image)
        
        reloadData()
    }

    private func setupUI(){
        
        self.hidden = true
    
        backgroundColor = UIColor.whiteColor()
        
        dataSource = self
        delegate  = self
        
        registerClass(GYComposePictureViewCell.self, forCellWithReuseIdentifier: GYComposePictureViewCellId)
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        let cellMargin: CGFloat = 5

        let cellWH: CGFloat = (SCREENW - 20 - 2*cellMargin)/3
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
    
    }

}
extension GYComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = imageList.count
        
        if count == 0 || count == 9 {
            
            return count
        }
        return imageList.count + 1
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(GYComposePictureViewCellId, forIndexPath: indexPath) as! GYComposePictureViewCell
        
        if imageList.count ==  indexPath.item {
            
            cell.image = nil
            
        }else {
            
            cell.image = imageList[indexPath.item]
            
            cell.closure = {[weak self] in
                
                self?.imageList.removeAtIndex(indexPath.item)
                
                if self?.imageList.count == 0 {
                    
                    self?.hidden = true
                }
                
                self?.reloadData()
                
            }
        }

        
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)

        if imageList.count ==  indexPath.item{

            closure?()
        }
    }
    

    
}
class GYComposePictureViewCell:UICollectionViewCell {
    
    var closure:(()->())?
    
    var image: UIImage?{
        didSet{
            
            if image == nil {
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteButton.hidden = true
                
            }else {
                
                bgImageView.image = image

                bgImageView.highlightedImage = nil
                
                deleteButton.hidden = false

            }

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
//        backgroundColor = RandomColor()
        
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.top.equalTo(bgImageView)
        }
        
    }
    
    private lazy var bgImageView: UIImageView = UIImageView()
    
    private lazy var deleteButton:UIButton = {
        
       let btn = UIButton()
        
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "deleteClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    @objc private func deleteClick(){
        
        print("删除图片")
        
        closure?()
    }
    
}
